package com.ship.boats.business;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ship.boats.domain.dto.CurveDataRq;
import com.ship.boats.domain.dto.QueryForwardRq;
import com.ship.boats.domain.dto.RequestInformationRq;
import com.ship.boats.service.CurrencyExchangeRateService;
import com.ship.boats.service.OilinessPriceService;
import com.ship.boats.util.LdentityAuthenticationUtil;
import com.ship.common.core.constant.ShipApiConstants;
import com.ship.common.core.utils.StringUtils;
import com.ship.common.core.web.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <b>CashForwardIntegrationBusiness</b>
 *
 * @description: CashForwardIntegrationBusiness <br>
 * @date: 2024/3/20 08:57 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Api(tags = "期现一体化模块")
@RestController
public class CashForwardIntegrationBusiness {
    private static final Logger logger = LoggerFactory.getLogger(CashForwardIntegrationBusiness.class);

    @Autowired
    private OilinessPriceService oilinessPriceService;

    @Autowired
    private CurrencyExchangeRateService currencyExchangeRateService;

    @ApiOperation("期现一体化数据对接")
    @PostMapping("/acquire/cash_forward")
    public JsonResult acquireCashForward(@RequestBody QueryForwardRq rq){
        logger.info("期现一体化推送数据内容:{}"+JSONUtil.parse(rq));
        String obj = LdentityAuthenticationUtil.decrypt(rq.getData(), "entrade-gaspower");
        logger.info("期现一体化推送数据内容解密结果:{}"+JSONUtil.parse(obj));
        JSONObject entries = JSONUtil.parseObj(obj);
        List<CurveDataRq> curveLists = JSONUtil.toList(JSONUtil.parseArray(entries.get("curveLists")),CurveDataRq.class);
        boolean status = false;
        if (CollectionUtil.isNotEmpty(curveLists)){
            for (CurveDataRq curveData : curveLists) {
                if (StringUtils.isNotEmpty(curveData.getUom()) && "Rate".equals(curveData.getUom())){
                    status = currencyExchangeRateService.createCashForwardData(curveData);
                }else {
                    status = oilinessPriceService.createCashForwardData(curveData);
                }
            }
        }
        return JsonResult.success(status);
    }
}
